package com.example.springsecurity.security.service;

import com.example.springsecurity.security.mapper.PermissionMapper;
import com.example.springsecurity.security.mapper.UserMapper;
import com.example.springsecurity.security.pojo.Permission;
import com.example.springsecurity.security.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class MyUserDetailsService implements UserDetailsService, SocialUserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //根据username查询数据库用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("用户名是 : {}",username);
        //根据用户名查询用户
        User user = userMapper.selectByName(username);
        if (user == null){
            logger.error("用户名{}不存在",username);
            throw new BadCredentialsException("用户不存在");
        }
        //存放用户权限
        List<Permission> permissions = permissionMapper.selectByUserId(user.getId());
        List<GrantedAuthority> GrantedAuthority = new ArrayList<>();
        //SimpleGrantedAuthority :是GrantedAuthority的一个实现类
        for (Permission permission : permissions) {
            GrantedAuthority.add(new SimpleGrantedAuthority(permission.getPermissionName()));
        }
        org.springframework.security.core.userdetails.User myUser = new org.springframework.security.core.userdetails.User(
                                                                            user.getUsername(),
                                                                            passwordEncoder.encode(user.getPassword()),
                                                                            GrantedAuthority);
        logger.info("用户信息 : {}",myUser);
        return myUser;
    }

    /**
     * qq时登陆使用userId登陆
     */
    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        String password = passwordEncoder.encode("123456");
        logger.info("数据库密码是:"+password);
        return new SocialUser(userId, password,
                true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
